import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../layout/index.vue'
import home from '../views/Home'
import store from '../store/index'
Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    name: 'index',
    redirect:'home',
    component: index,
    children:[{
      path:'',
      component:home
    }, {
      path: '/CourseDetail',
      component: () => import("../views/CourseDetail")
    },
      {
        path:'/shopping',
        component:()=>import("../views/shopping")
      }]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
let getMessage = store.dispatch('getMessage')
router.beforeEach((to,from,next)=>{
  if(to.meta.needLogin){
    getMessage.then(()=>{
      if(store.state.logAfter){
        next()
      }else{
        store.commit('changMT',true)
      }
    })
  }else{
    next()
  }
})
export default router
